package future

// 2个库：github.com/chebyrash/promise
//       github.com/fanliao/go-promise

import (
	"context"
	"fmt"
	"github.com/chebyrash/promise"
	"time"
	//g_promise "github.com/fanliao/go-promise"
)

func chebyrash() {
	ctx := context.Background()
	p := promise.New(func(resolve func(interface{}), reject func(error)) {
		// 异步工作
		result, err := someAsyncWork(ctx)
		if err != nil {
			reject(err)
			return
		}
		resolve(result)
	})

	result, err := p.Await(ctx)
	if err != nil {
		fmt.Println("Error:", err)
	} else {
		fmt.Println("Result:", result)
	}
}

func someAsyncWork(ctx context.Context) (interface{}, error) {

	time.Sleep(4000 * time.Millisecond)

	return 4444, nil
}

//func Gpromise() {
//	future := g_promise.NewFuture()
//
//	go func() {
//		result, err := someAsyncWork()
//		if err != nil {
//			future.Reject(err)
//		} else {
//			future.Resolve(result)
//		}
//	}()
//
//	result, err := future.Get()
//	// 处理结果
//}
